/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import beans.SeekerCustomer;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Dhruv
 */

//register an account
public class CreateUserAction extends Action {

    //constructor
    public CreateUserAction(SeekerCustomer model) {
        super(model);
    }

    //identifies the operation name
    public String getName() {
        return "CreateUser";
    }

    public Object perform(HttpServletRequest req, HttpServletResponse resp) {

        //JSP page to dispatch the request to
        String address ="";

        //if one of the name or password fiels is empty, go to error page
        if(req.getParameter("newName").equals("") || req.getParameter("newPassword").equals(""))
        {
            address = "Error.jsp";            
            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("error", "You didn't enter username or password or both.");
        }
        //if username exists, choose a new one
        else if(model.isExisting(req.getParameter("newName")))
        {
            address = "Error.jsp";            
            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("error", "This username exists already. Choose a new one please.");
        }
        //allow new user creation
        else{
            address = "ValidateUser.jsp";
            //insert new customer into database
            model.setCustomer(req.getParameter("newName"), req.getParameter("newPassword"), req.getParameter("newPic"));
            //retrieve new customer from database
            model = SeekerCustomer.getCustomer(req.getParameter("newName"));
            //get customer's name
            String myName = model.getName();
            //get customer's picture
            String myPicture = model.getPicture();

            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("myName", myName);
            req.setAttribute("myPicture", myPicture);            
        }

        //below object receives requests from the client and sends them to JSP address
        RequestDispatcher dispatcher = req.getRequestDispatcher(address);
        try {
            dispatcher.forward(req, resp);
        } catch (ServletException ex) {
            Logger.getLogger(CreateUserAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
              Logger.getLogger(CreateUserAction.class.getName()).log(Level.SEVERE, null, ex);
        }

        //returning the object conforms to the Abstract Action class definition
        //object needed to maintain session between requests
        return model;
    }
}